<?php
class shop_cartClass {

	function shop_cartClass () {
		global $setupdata, $mysqlclass, $treeclass, $shopclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
		$this->treeclass=$treeclass;
		$this->shopclass=$shopclass;
	}
	
	function get_all_source_types(){
		$result = array();
		if (isset($this->setupdata['smarty_guest_mail_template_dir']) && (strlen($this->setupdata['smarty_guest_mail_template_dir']) > 0)){
			if (file_exists($this->setupdata['smarty_guest_mail_template_dir'])) $dir = dir($this->setupdata['smarty_guest_mail_template_dir']);
			else return $result;
		} else {
			if (file_exists("templates/guest/mail")) $dir = dir("templates/guest/mail");
			else return $result;
		}
		while (false !== ($file = $dir->read())) {
			if ($file != '.' && $file != '..') {
				if (!is_dir($file)) $result[$file] = $file;
			}
		}
		$dir->close();
		asort($result);
		return $result;
	}
	
	function ClearCart($cart_id, $group='0'){
		if (strlen($cart_id) > 0){
			if ($group == '0') $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."shop_cart` WHERE `user_id` = '".$cart_id."' OR `user_id` = '".session_id()."'", __LINE__, __FILE__);
			else $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."shop_cart` WHERE `user_id` = '".$cart_id."' AND `group`='".$group."'", __LINE__, __FILE__);
		}
	}
			
	function AddCart($cart_id, $product, $count, $source, $group='0', $product_custom){
		$count = intval($count);
		if ((strlen($product) > 0) and (strlen($cart_id) > 0) and ($count > 0)){
			if ($group == '0') {
			 	$this->AddCartOnce($cart_id, $product, $count, $source, '0', $product_custom);
			} else {
				$this->AddCartOnce($cart_id, $product, $count, $source, $group, $product_custom);
			}
		}
		$this->DelTimeOut();
	}

	function IsInsertToCart($product_id){
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE `id` = ".$product_id, __LINE__, __FILE__);		
		$row = mysql_fetch_array($sql, MYSQL_ASSOC);
		if (in_array($row['type'], $this->setupdata['modules']['shop']['once'])) return true;
		else return false;
	}
	
	function IsMathToCart($product_id){
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE `id` = ".$product_id, __LINE__, __FILE__);		
		$row = mysql_fetch_array($sql, MYSQL_ASSOC);
		if (in_array($row['type'], $this->setupdata['modules']['shop']['not_in_cart'])) return true;
		else return false;
	}
	
	function AddCartOnce($cart_id, $product, $count, $source, $group='0', $product_custom){
		$count = intval($count);
		if ($product_custom != NULL) $product_custom = addslashes(serialize($product_custom));
		else $product_custom = "";
		if (strlen($count) == 0) $count = 0;
		$product = intval($product);
		if ((strlen($product) > 0) && ($product > 0) && (strlen($cart_id) > 0)){
			$dat = date("YmdHis");
			
			if (isset($this->setupdata['modules']['shop']['once'])){
				$is_delete = $this->IsInsertToCart($product);
			} else $is_delete = false;
			
			if ($group == '0') {
				if ($is_delete) {
					$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart` WHERE `user_id` = '".$cart_id."'", __LINE__, __FILE__);
					while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
						if ($this->IsInsertToCart($row['product_id'])) $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."shop_cart` WHERE `user_id` = '".$cart_id."' AND `product_id` = ".$row['product_id'], __LINE__, __FILE__);
					}
				}
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart` WHERE `user_id` = '".$cart_id."' AND `product_id` = ".$product, __LINE__, __FILE__);
				$row = mysql_num_rows($sql);
				if ($row > 0) {
					$sql = "UPDATE `".$this->setupdata['table_prefix']."shop_cart` SET product_count='".$count."', add_time='".$dat."', custom='".$product_custom."' WHERE `user_id` = '".$cart_id."' AND `product_id` = ".$product;
					$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
				} else {
					$sql = "INSERT INTO `".$this->setupdata['table_prefix']."shop_cart` (`user_id`,`product_id`,`product_count`,`add_time`,`source`,`custom`) VALUES ('".$cart_id."', '".$product."', ".$count.", '".$dat."', '".$source."', '".$product_custom."')";
					$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
				}
			} else {
				if ($is_delete) {
					$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart` WHERE `user_id` = '".$cart_id."' AND `group`='".$group."'", __LINE__, __FILE__);
					while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
						if ($this->IsInsertToCart($row['product_id'])) $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."shop_cart` WHERE `user_id` = '".$cart_id."' AND `product_id` = ".$row['product_id']." AND `group`='".$group."'", __LINE__, __FILE__);
					}
				}
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart` WHERE `user_id` = '".$cart_id."' AND `product_id` = ".$product." AND `group`='".$group."'", __LINE__, __FILE__);
				$row = mysql_num_rows($sql);
				if ($row > 0) {
					$sql = "UPDATE `".$this->setupdata['table_prefix']."shop_cart` SET product_count='".$count."', add_time='".$dat."', custom='".$product_custom."' WHERE `user_id` = '".$cart_id."' AND `product_id` = ".$product." AND `group`='".$group."'";
					$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
				} else {
					$sql = "INSERT INTO `".$this->setupdata['table_prefix']."shop_cart` (`user_id`,`product_id`,`product_count`,`add_time`, `group`,`source`,`custom`) VALUES ('".$cart_id."', '".$product."', ".$count.", '".$dat."', '".$group."', '".$source."', '".$product_custom."')";
					$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
				}
			}
		}
	}
	
	function GetCartGroups($user_id){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT DISTINCT `group` FROM `".$this->setupdata['table_prefix']."shop_cart` WHERE `user_id` = '".$user_id."'", __LINE__, __FILE__);		
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			if (strlen($row['group']) > 0)	$result[$row['group']] = $row['group'];
		}
		return $result;
	}
	
	function GetCart($cart_id, $group='0', $lang){
		global $shopclass;
		$shopclass->get_all_offs();
		$this->DelTimeOut();
		$rows = array();
		$rows['total_count'] = 0;
		if (strlen($cart_id) > 0){
			if ($group == '0') $sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart` WHERE `user_id` = '".$cart_id."' OR `user_id` = '".session_id()."'", __LINE__, __FILE__);
			else $sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart` WHERE `user_id` = '".$cart_id."' AND `group`='".$group."'", __LINE__, __FILE__);
			$num = mysql_num_rows($sql);
			if ($num > 0) while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
				$arr = array();
				$arr = $shopclass->get_product_fields_with_data($row['product_id'], $lang);
				foreach ($arr as $key=>$val) {
					if ($key == 'price') {
						$row['price'] = $val;
						$row['price']['price_all'] = $row['price']['price']*$row['product_count'];
						$row['price']['total_all'] = $row['price']['total']*$row['product_count'];
						$row['price']['total_all_not_pvm'] = $row['price']['not_pvm']*$row['product_count'];
					} else {
						$row['data'][$key] = $val;
					}
				}
				
				$row['custom'] = unserialize($row['custom']);
				if ($row['custom'])	foreach($row['custom'] as $key=>$value) $row['custom'][$key] = stripslashes($value);
				if (isset($this->setupdata['modules']['shop']['not_in_cart'])){
					if (!(in_array($row['data']['product_type'], $this->setupdata['modules']['shop']['not_in_cart'])))
						$rows['total_count'] = $rows['total_count'] + $row['product_count'];
				} else $rows['total_count'] = $rows['total_count'] + $row['product_count'];
				$rows[$row['product_id']] = $row;
			}
		}
		return $rows;
	}
	
	function RemoveCart($cart_id, $product, $group='0'){
		if ((strlen($product) > 0) and (strlen($cart_id) > 0)){
			if ($group == '0') $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."shop_cart` WHERE `user_id` = '".$cart_id."' AND `product_id` = ".$product, __LINE__, __FILE__);
			else $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."shop_cart` WHERE `user_id` = '".$cart_id."' AND `product_id` = ".$product." AND `group`='".$group."'", __LINE__, __FILE__);
		}
	}
	
	function DelTimeOut(){
		$tm = date("YmdHis");
	  	$timeout = 86400;
		$sql = $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."shop_cart` WHERE ".$tm."-`add_time` > ".$timeout." AND `group`=''", __LINE__, __FILE__);
	}
	
	function GetLastOrder(){
		$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(order_id) from `".$this->setupdata['table_prefix']."shop_cart_order`", __LINE__, __FILE__);
		$row = mysql_fetch_array($sql);
		if ($row[0] = NULL) return 1;
		else return $row[0];
	}
	
	function insert_product_data($order_id, $product_id, $custom){
		$data = array();
		global $shopclass;
		$data = $shopclass->get_product_fields_with_data_and_type($product_id, $_SESSION['lang']);
		if (count($data) > 0){
			foreach ($data as $key=>$value) {
				if (($key != 'data') && ($key != 'price')){
					if ((!is_array($value)) or (!isset($value['value'])) or (!isset($value['type']))) {
						$val = $value;
						$value = array();
						$value['type'] = 'text';
						$value['value'] = $val;
					}
					$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."shop_cart_products_data` WHERE `order_id`=".$order_id, __LINE__, __FILE__);
					if (mysql_num_rows($sql) > 0) {
						$row = mysql_fetch_array($sql);
						$pos = $row[0]+1;
					} else $pos = 0;
					if (is_string($value['value'])) $value['value'] = addslashes($value['value']);
					$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_products_data` (`id`, `order_id`, `product_id`, `name`, `value`, `type`, `pos`) VALUES ('', ".$order_id.", ".$product_id.", '".$key."', '".$value['value']."', '".$value['type']."', ".$pos.")", __LINE__, __FILE__);
				} elseif ($key == 'price'){
					foreach ($value as $k=>$v) {
						$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."shop_cart_products_data` WHERE `order_id`=".$order_id, __LINE__, __FILE__);
						if (mysql_num_rows($sql) > 0) {
							$row = mysql_fetch_array($sql);
							$pos = $row[0]+1;
						} else $pos = 0;
						$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_products_data` (`id`, `order_id`, `product_id`, `name`, `value`, `type`, `pos`) VALUES ('', ".$order_id.", ".$product_id.", '".$k."', '".$v."', 'price_info', ".$pos.")", __LINE__, __FILE__);
					}
				}
			}
		}
		if (is_array($custom) && (count($custom) > 0)){
			foreach ($custom as $key=>$value) {
				$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."shop_cart_products_data` WHERE `order_id`=".$order_id, __LINE__, __FILE__);
				if (mysql_num_rows($sql) > 0) {
					$row = mysql_fetch_array($sql);
					$pos = $row[0]+1;
				} else $pos = 0;
				$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_products_data` (`id`, `order_id`, `product_id`, `name`, `value`, `type`, `pos`) VALUES ('', ".$order_id.", ".$product_id.", '".$key."', '".$value."', 'custom_info', ".$pos.")", __LINE__, __FILE__);
			}
		}
	}
	
	function OrderCart($cart_id, $cart_data, $cart_info, $lang, $user_id, $group='0', $off_info='0'){
		global $clientsclass, $shopclass;
		if (strlen($cart_id) > 0){
			$offs = $shopclass->offs;
			if ($user_id != 0){
				if ($group == '0') $card = $this->GetCart($user_id, 0, $lang);
				else $card = $this->GetCart($user_id, $group, $lang);
			} else {
				if ($group == '0') $card = $this->GetCart($cart_id, 0, $lang);
				else $card = $this->GetCart($cart_id, $group, $lang);
			}
			if (count($card) > 0){
				$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_order` (`order_id`, `date`, `status`, `lang`, `modif`, `time_modif`, `archive`) VALUES ('', NOW(), '0', '".strtolower($_SESSION['lang'])."', 0, NOW(), 0)", __LINE__, __FILE__);
				$order_id = mysql_insert_id();
				foreach ($card as $key=>$value) {
					if ($key != 'total_count'){
						$product_id = $key;
						$count = $value['product_count'];
						$data = $shopclass->get_product_fields_with_data($product_id, $lang);
						if (isset($data['price']['price'])) $cost = $data['price']['price'];
						else $cost = 0;
						$info = $shopclass->get_product_fields($product_id);
						$product_name = $info['data']['name'];
						$product_name = addslashes($product_name);
						$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_products` (`id`, `order_id`, `product_id`, `product_name`, `cost`, `count`) VALUES ('', ".$order_id.", ".$product_id.", '".$product_name."', '".$cost."', ".$count.")", __LINE__, __FILE__);
						$this->insert_product_data($order_id, $product_id, $value['custom']);
						if (is_array($offs)) {
							if (isset($data['price']['discount_id'])){
								if ((isset($offs['global']['id'])) && ($offs['global']['id'] == $data['price']['discount_id'])) $off = $offs['global'];
								elseif ((isset($offs['client']['id'])) && ($offs['client']['id'] == $data['price']['discount_id'])) $off = $offs['client'];
								if (!isset($off)){
									foreach ($offs['indivi'] as $key_off=>$value_off) {
										if ($key_off == $data['price']['discount_id']) $off = $offs['indivi'][$key_off];
									}
								}
								if (!isset($off)){
									foreach ($offs['select'] as $key_off=>$value_off) {
										if ($key_off == $data['price']['discount_id']) $off = $offs['select'][$key_off];
									}
								}
								if (isset($off)){
									$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."shop_cart_products_data` WHERE `order_id`=".$order_id, __LINE__, __FILE__);
									if (mysql_num_rows($sql) > 0) {
										$row = mysql_fetch_array($sql);
										$pos = $row[0]+1;
									} else $pos = 0;
									$off['name'] = addslashes($off['name']);
									$off['desc'] = addslashes($off['desc']);
									$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_products_data` (`id`, `order_id`, `product_id`, `name`, `value`, `type`, `pos`) VALUES ('', ".$order_id.", ".$product_id.", 'id', '".$off['id']."', 'off', ".$pos.")", __LINE__, __FILE__);
									$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_products_data` (`id`, `order_id`, `product_id`, `name`, `value`, `type`, `pos`) VALUES ('', ".$order_id.", ".$product_id.", 'name', '".$off['name']."', 'off', ".intval($pos+1).")", __LINE__, __FILE__);
									$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_products_data` (`id`, `order_id`, `product_id`, `name`, `value`, `type`, `pos`) VALUES ('', ".$order_id.", ".$product_id.", 'desc', '".$off['desc']."', 'off', ".intval($pos+2).")", __LINE__, __FILE__);
									$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_products_data` (`id`, `order_id`, `product_id`, `name`, `value`, `type`, `pos`) VALUES ('', ".$order_id.", ".$product_id.", 'num', '".$off['num']."', 'off', ".intval($pos+3).")", __LINE__, __FILE__);
								}
							}
						}
					}
				}
				foreach ($cart_data as $value) {
					if ($value['type'] == 'checkbox') $value['value'] = 'on';
					$value['label'] = trim($value['label']);
					$value['label'] = addslashes($value['label']);
					$value['value'] = trim($value['value']);
					$value['value'] = addslashes($value['value']);
					$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."shop_cart_data` WHERE `order_id`=".$order_id, __LINE__, __FILE__);
					if (mysql_num_rows($sql) > 0) {
						$row = mysql_fetch_array($sql);
						$pos = $row[0]+1;
					} else $pos = 0;
					$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_data` (`id`, `order_id`, `name`, `value`, `type`, `pos`, `group`) VALUES ('', ".$order_id.", '".$value['label']."', '".$value['value']."', '".$value['type']."', ".$pos.", 'data')", __LINE__, __FILE__);
				}
				
				/*
				foreach ($cart_info as $key=>$value) {
					$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."shop_cart_data` WHERE `order_id`=".$order_id, __LINE__, __FILE__);
					if (mysql_num_rows($sql) > 0) {
						$row = mysql_fetch_array($sql);
						$pos = $row[0]+1;
					} else $pos = 0;
					$value = trim($value);
					$value = addslashes($value);
					$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_data` (`id`, `order_id`, `name`, `value`, `type`, `pos`, `group`) VALUES ('', ".$order_id.", '".$key."', '".$value."', 'text', ".$pos.", 'info')", __LINE__, __FILE__);
				}*/
				
				foreach ($cart_info as $key=>$value) {
					$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."shop_cart_data` WHERE `order_id`=".$order_id, __LINE__, __FILE__);
					if (mysql_num_rows($sql) > 0) {
						$row = mysql_fetch_array($sql);
						$pos = $row[0]+1;
					} else $pos = 0;
					
					foreach ($value as $key2=>$value2) {
						$value2 = trim($value2);
						$value2 = addslashes($value2);
						$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_data` (`id`, `order_id`, `name`, `value`, `type`, `pos`, `group`) VALUES ('', ".$order_id.", '".$key2."', '".$value2."', 'text', ".$pos.", 'info')", __LINE__, __FILE__);
					}
					
				}
				
				if (is_array($offs)) {
					if (isset($offs['global'])){
						foreach ($offs['global'] as $key=>$value) {
							$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."shop_cart_data` WHERE `order_id`=".$order_id, __LINE__, __FILE__);
							if (mysql_num_rows($sql) > 0) {
								$row = mysql_fetch_array($sql);
								$pos = $row[0]+1;
							} else $pos = 0;
							$value = trim($value);
							$value = addslashes($value);
							$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_data` (`id`, `order_id`, `name`, `value`, `type`, `pos`, `group`) VALUES ('', ".$order_id.", '".$key."', '".$value."', 'text', ".$pos.", 'off_global')", __LINE__, __FILE__);
						}
					}
					if (isset($offs['select'])){
						foreach ($offs['select'] as $key=>$value) {
							if ($value['selected'] == 1){
								foreach ($value as $k=>$v) {
									$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."shop_cart_data` WHERE `order_id`=".$order_id, __LINE__, __FILE__);
									if (mysql_num_rows($sql) > 0) {
										$row = mysql_fetch_array($sql);
										$pos = $row[0]+1;
									} else $pos = 0;
									$v = trim($v);
									$v = addslashes($v);
									$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_data` (`id`, `order_id`, `name`, `value`, `type`, `pos`, `group`) VALUES ('', ".$order_id.", '".$k."', '".$v."', 'text', ".$pos.", 'off_select')", __LINE__, __FILE__);
								}
							}
						}
					}
					if ($user_id != 0){
						$offs['client'] = $shopclass->off_client;
						if (is_array($offs['client'])){
							foreach ($offs['client'] as $key=>$value) {
								$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."shop_cart_data` WHERE `order_id`=".$order_id, __LINE__, __FILE__);
								if (mysql_num_rows($sql) > 0) {
									$row = mysql_fetch_array($sql);
									$pos = $row[0]+1;
								} else $pos = 0;
								$value = trim($value);
								$value = addslashes($value);
								$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_data` (`id`, `order_id`, `name`, `value`, `type`, `pos`, `group`) VALUES ('', ".$order_id.", '".$key."', '".$value."', 'text', ".$pos.", 'off_client')", __LINE__, __FILE__);
							}
						}
					}
				}
				
				if ($user_id != 0){
					$result = array();
					$info = $clientsclass->get_clients_info_by_id($user_id);
					if (isset($info['data'])){
						$result = $info['data'];
						$result['email']['value'] = $info['main']['email'];
						$result['email']['type'] = 'email';
						$result['client_id']['value'] = $info['main']['id'];
						$result['client_id']['type'] = 'client_id';
	
						foreach ($result as $key=>$value) {
							$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."shop_cart_data` WHERE `order_id`=".$order_id, __LINE__, __FILE__);
							if (mysql_num_rows($sql) > 0) {
								$row = mysql_fetch_array($sql);
								$pos = $row[0]+1;
							} else $pos = 0;
							$value['value'] = addslashes($value['value']);
							$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_data` (`id`, `order_id`, `name`, `value`, `type`, `pos`, `group`) VALUES ('', ".$order_id.", '".$key."', '".$value['value']."', '".$value['type']."', ".$pos.", 'client_info')", __LINE__, __FILE__);
						}
					}
				}
				
				if ($user_id != 0) $this->send_cart_to_admin($order_id, $card, $result);
				else $this->send_cart_to_admin($order_id, $card, 0);
				
				if ($group == 0) $this->ClearCart($cart_id);
				else $this->ClearCart($cart_id, $group);
				
				if (isset($this->setupdata['modules']['payment']) && ($this->setupdata['modules']['payment']['is'] == 1)){
					if ((isset($_POST['payment'])) && (is_array($_POST['payment'])) && (count($_POST['payment']) > 0) && (isset($order_id))){
						global $paymentclass;
						$payment = $_POST['payment'];
						$payment['data']['order_id'] = $order_id;
						$payment['data']['order_from'] = 'shop_cart';
						$payment['data']['order_amount'] = 0;
						unset($card['total_count']);
						foreach ($card as $value) {
							$payment['data']['order_amount'] = $payment['data']['order_amount'] + $value['price']['total_all'];
							if (strpos($this->setupdata['base_href'], 'magazines') !== false){
								if (isset($value['custom']['air']) && (intval($value['custom']['air']) > 0)){
									$payment['data']['order_amount'] = $payment['data']['order_amount'] + $value['custom']['air'] + round($value['custom']['air']*0.18, 2);
								}
							}
						}
						$post['post'] = $_POST;
						$post['data'] = $card;
						
						$paymentclass->sent_order_to_gateway($payment['gateway'], $payment['data'], $post);
					}
				}
				if (method_exists($this->treeclass,'save_event')) $this->treeclass->save_event('shop_cart', 'ordered', $order_id);
			}
		}
		return 0;
	}
	
	function get_all_orders(){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart_order` ORDER BY `date` DESC", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$row['suma'] = 0;
			$row['count'] = 0;
			$sql2 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart_products` WHERE `order_id`=".$row['order_id'], __LINE__, __FILE__);
			while ($row2 = mysql_fetch_array($sql2, MYSQL_ASSOC)) {
				$row['suma'] = $row['suma'] + $row2['cost']*$row2['count'];
				$row['count'] = $row['count'] + $row2['count'];
			}
			$sql3 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart_data` WHERE `order_id`=".$row['order_id']." AND `name`='client_id' AND `type`='client_id' AND `group`='client_info'", __LINE__, __FILE__);
			if (mysql_num_rows($sql3) == 1) {
				$row3 = mysql_fetch_array($sql3, MYSQL_ASSOC);
				$row['client_id'] = $row3['value'];
			} else $row['client_id'] = 0;
			$result[$row['order_id']] = $row;
		}
		return $result;
	}
	
	function change_order_status($order_id, $status){
		$sql = "UPDATE `".$this->setupdata['table_prefix']."shop_cart_order` SET `modif`=".$_SESSION['login_user'].", status='".$status."', time_modif='".date("YmdHis")."' WHERE `order_id` = ".$order_id;
		$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
	}
	
	function get_order_data($order_id){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart_order` WHERE `order_id`=".$order_id, __LINE__, __FILE__);
		if (mysql_num_rows($sql) == 1) $result['main'] = mysql_fetch_array($sql, MYSQL_ASSOC);
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart_data` WHERE `order_id`=".$order_id, __LINE__, __FILE__);
		if (mysql_num_rows($sql) > 0) {
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
				if ($row['group'] == 'off_global'){
					$result[$row['group']][stripslashes($row['name'])] = stripslashes($row['value']);
				} elseif ($row['group'] == 'off_select'){
					$result[$row['group']][stripslashes($row['name'])] = stripslashes($row['value']);
				} elseif ($row['group'] == 'off_client'){
					$result[$row['group']][stripslashes($row['name'])] = stripslashes($row['value']);
				} else $result[$row['group']][$row['id']] = $row;
			}
		} else $result['data'] = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart_products` WHERE `order_id`=".$order_id, __LINE__, __FILE__);
		if (mysql_num_rows($sql) > 0) {
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
				$result['products'][$row['id']] = $row;
				$result['products'][$row['id']]['name'] = stripslashes($row['product_name']);
				$result['products'][$row['id']]['data'] = $this->get_order_products_data($order_id, $row['product_id']);
				if (!isset($result['products'][$row['id']]['data']['price']['price'])) $result['products'][$row['id']]['data']['price']['price'] = 0;
				if (!isset($result['products'][$row['id']]['data']['price']['total'])) $result['products'][$row['id']]['data']['price']['total'] = 0;
				$result['products'][$row['id']]['data']['price']['price_all'] = $result['products'][$row['id']]['data']['price']['price']*$row['count'];
				$result['products'][$row['id']]['data']['price']['total_all'] = $result['products'][$row['id']]['data']['price']['total']*$row['count'];
			}
		} else $result['products'] = array();
		return $result;
	}
	
	function orders_del($array){
		foreach($array as $elem){
			if (isset($elem) and ($elem > 0)){
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."shop_cart_order` WHERE ".$elem." = `order_id`", __LINE__, __FILE__);		
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."shop_cart_products` WHERE ".$elem." = `order_id`", __LINE__, __FILE__);		
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."shop_cart_data` WHERE ".$elem." = `order_id`", __LINE__, __FILE__);		
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."shop_cart_products_data` WHERE ".$elem." = `order_id`", __LINE__, __FILE__);		
			}
		}
	}
	
	function orders_archive($array, $status){
		foreach($array as $elem){
			if (isset($elem) and ($elem > 0)){
				$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."shop_cart_order` SET `archive`=".$status." WHERE ".$elem." = `order_id`", __LINE__, __FILE__);
			}
		}
	}
	
	function get_order_products_data($order_id, $product_id){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart_products_data` WHERE `order_id`=".$order_id." AND `product_id`=".$product_id, __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			if ($row['type'] == 'off'){
				$result['off'][stripslashes($row['name'])] = stripslashes($row['value']);
			} elseif ($row['type'] == 'price_info'){
				$result['price'][stripslashes($row['name'])] = stripslashes($row['value']);
			} else {
				$result[stripslashes($row['name'])]['value'] = stripslashes($row['value']);
				$result[stripslashes($row['name'])]['type'] = $row['type'];
			}
		}
		return $result;
	}
	
	function add_to_stat($order_id){
		$info = $this->get_order_data($order_id);
		foreach($info['products'] as $product){
			$suma = intval($product['count']) * floatval($product['cost']);
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart_stats` WHERE `product_id`='".$product['product_id']."'", __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0) {
				$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."shop_cart_stats` SET `name`='".$product['product_name']."', `suma`=`suma`+".$suma.", `count`=`count`+".intval($product['count'])." WHERE ".$product['product_id']." = `product_id`", __LINE__, __FILE__);
			} else {
				$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_stats` (`product_id`, `name`, `suma`, `count`) VALUES (".$product['product_id'].", '".$product['product_name']."', '".$suma."', '".intval($product['count'])."')", __LINE__, __FILE__);
			}
		}
		$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."shop_cart_order` SET `is_in_stat`='1' WHERE ".$order_id." = `order_id`", __LINE__, __FILE__);
	}
	
	function get_statistics(){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart_stats`", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			$row['name']  = stripslashes($row['name']);
			$result[$row['product_id']] = $row;
		}
		return $result;
	}
	
	function remove_to_stat($order_id){
		$info = $this->get_order_data($order_id);
		foreach($info['products'] as $product){
			$suma = intval($product['count']) * floatval($product['cost']);
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart_stats` WHERE `product_id`='".$product['product_id']."'", __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0) {
				$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."shop_cart_stats` SET `suma`=`suma`-".$suma.", `count`=`count`-".intval($product['count'])." WHERE ".$product['product_id']." = `product_id`", __LINE__, __FILE__);
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."shop_cart_stats` WHERE `product_id`='".$product['product_id']."'", __LINE__, __FILE__);
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."shop_cart_stats` WHERE `count`=0", __LINE__, __FILE__);	
			}
		}
		$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."shop_cart_order` SET `is_in_stat`='0' WHERE ".$order_id." = `order_id`", __LINE__, __FILE__);
	}
	
	function get_last_used_group_cart_by_user($user_id){
		$sql = $this->mysqlclass->mysql_my_query("SELECT `group` FROM `".$this->setupdata['table_prefix']."shop_cart` WHERE `user_id` = '".$user_id."' ORDER BY `add_time` DESC", __LINE__, __FILE__);
		$row = mysql_fetch_array($sql, MYSQL_ASSOC);
		return $row;
	}
	
	function reset_stats(){
		$this->mysqlclass->mysql_my_query("TRUNCATE TABLE `".$this->setupdata['table_prefix']."shop_cart_stats`", __LINE__, __FILE__);	
		$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."shop_cart_order` SET `is_in_stat`='0'", __LINE__, __FILE__);		
	}
	
	function mod_settings($to, $subject, $template){
		$to = addslashes($to);	
		$subject = addslashes($subject);	
		$template = addslashes($template);	
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."object_data` WHERE `group` = 'cart_settings'", __LINE__, __FILE__);		
		$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."object_data` (`id`,`parent`,`name`,`value`,`type`,`pos`,`group`) VALUES ('', 0, 'cart_to', '".$to."', 'text', 0, 'cart_settings')", __LINE__, __FILE__);
		$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."object_data` (`id`,`parent`,`name`,`value`,`type`,`pos`,`group`) VALUES ('', 0, 'cart_subject', '".$subject."', 'text', 0, 'cart_settings')", __LINE__, __FILE__);
		$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."object_data` (`id`,`parent`,`name`,`value`,`type`,`pos`,`group`) VALUES ('', 0, 'cart_template', '".$template."', 'text', 0, 'cart_settings')", __LINE__, __FILE__);
	}
	
	function get_mod_settings(){
		$arr = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."object_data` WHERE `group`='cart_settings'", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$arr[$row['name']] = stripslashes($row['value']);
		}	
		return $arr;
	}

	function send_cart_to_admin($order_id, $cart, $user_info){
		global $smarty;
		$cart_settings = $this->get_mod_settings();
		if ($user_info != 0) $cart_info['user_info'] = $user_info;
		else $cart_info['user_info'] = array();
		$result = 0;
		$cart_info['order_id'] = $order_id;
		$cart_info['cart'] = $cart;
		if (isset($cart_settings['cart_to']) && isset($cart_settings['cart_subject']) && isset($cart_settings['cart_template']) && (strlen($cart_settings['cart_to']) > 4) && (strlen($cart_settings['cart_subject']) > 0) && (strlen($cart_settings['cart_template']) > 0)){
			$smarty->assign("cart_info", $cart_info);
			$tempalate = "guest/mail/".$cart_settings['cart_template'];
			if ($smarty->template_exists($tempalate)) $output = $smarty->fetch($tempalate);
			$output_text = strip_tags($output);
			$mail_settings['form_to'] = $cart_settings['cart_to'];
			$mail_settings['form_subject'] = $cart_settings['cart_subject'];
			$result = $this->treeclass->send_form_to_mail($output, $output_text, $mail_settings, array());
			$smarty->clear_assign("cart_info");
		}
		return $result;
	}
}
?>